const app = getApp()
const $api = require('../../../utils/api.js').API;
var common = require('../../../utils/common.js');
Page({
    data: {
      form:{},
      avatar:'',
      train:[],
      jobCate:{},
      business:{},
      education:['高中以下','中专','大专','本科','硕士以上'],
      index:0,
      workTime: ['无', '1年', '2年', '3年','4年','5年以上'],
      index1:0,
      submittrue:true,
      studyList:[
        {'name':'培训学校'},
        {'name':'实体店面'},
        {'name':'管理公司'},
        {'name':'美业厂家'},
        {'name':'无'},
      ],
      img1:'/static/img/pub.png',
      uploadtrue:false,
      imgUrl:app.globalData.imgUrl,
    },
    onShow: function(t) {
      this.setData({
          avatar:wx.getStorageSync('avatarUrl')
      })
      wx.showLoading()
      this.getInfo();
    },
    getInfo:function(){
      var data = {'uid':wx.getStorageSync('uid')}
      var that = this
      $api.get('user/info',data).then(res => {
          wx.hideLoading()
          //请求成功
          if(res.data.code == 1){
            var form = res.data.data
            if(wx.getStorageSync('city')){
              form.city = wx.getStorageSync('city')
            }
            if(wx.getStorageSync('post')){
              form.post = wx.getStorageSync('post')
            }
            that.setData({
              form:form
            })
            if(form.realPic){
              that.setData({
                img1:that.data.imgUrl+form.realPic,
              })
            }
            that.getTrain()
            that.cateData(that.data.studyList,'studyList')
            that.getIndex(that.data.education,'education','index')
            that.getIndex(that.data.workTime,'workTime','index1')
          }
      })
    },

    getTrain:function(){
      var data = {}
      var that = this
      $api.get('job/getTrain',data).then(res =>{
        if(res.data.code == 1){
          that.cateData(res.data.data,'business');
        }
      })
    },
    getIndex:function(list,key,index){
      var kk = 0;
      if(this.data.form[key]){
        for(var x in list){
          if(list[x] == this.data.form[key]){
            var kk = x;
          }
        }
      }
      this.setData({
        [index]:kk
      })
    },

    cateData:function(cate,key){
      var that = this
      var form = this.data.form
      if(form[key]){
        let post = form[key].split(',')
        for(var x in cate){
          if(post.indexOf(cate[x]['name']) >= 0){
            cate[x]['checked'] = true
          }
        }
      }
      that.setData({
        [key]:cate
      })
    },
    bindAllInput: function (e) {
      var inputName = e.currentTarget.dataset.name;
      var form = this.data.form
      form[inputName] = e.detail.value
      this.setData({
        form:form
      })
    },
    bindBox: function (e) {
      var inputName = e.currentTarget.dataset.name;
      var value = e.detail.value
  
      let form = this.data.form
      form[inputName] = value.join(',');

      this.setData({
        form:form
      })
    },
    bindChange: function(e){
      var inputName = e.currentTarget.dataset.name;
      var index = e.currentTarget.dataset.key
      var value = e.detail.value
      var form = this.data.form
      form[inputName] = this.data[inputName][value]
      this.setData({
        form:form,
        [index]:value
      })
    },
    infoAdd:function(){
      var array = {
          'realname':'姓名必填',
          'sex':'性别必选',
          'workType':'工作状态必填',
          'post':'期望工作岗位必填',
          'money':'期望工作薪资',
          'business':'培训经历必选',
          'city':'期望工作城市必选',
          'studyList':'培训经历必选'
      }
      var message = common.message(array,this.data.form);
      if(message){
        wx.showToast({
          title: message,//提示文字
          duration:3000,//显示时长
          mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon:'none', //图标，支持"success"、"loading"  
       })
        return;
      }
      //调接口 提交
      this.setData({
        submittrue:false
      })
      var that = this
      $api.put('user/edit',this.data.form).then(res => {
          //请求成功
          if(res.data.code == 1){
            wx.removeStorageSync('post')
            wx.removeStorageSync('city')
            wx.showToast({
              title: '修改成功',//提示文字 advantage
              duration:3000,//显示时长
              mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
              icon:'none', //图标，支持"success"、"loading" 
              success:function(){
                setTimeout(function(){
                    wx.switchTab({
                      url: '../index'
                    })
                  },3000) //延迟时间 这里是
              },//接口调用成功 
           })
          }
      })
    },
    goCenter:function(e){
      var url = e.currentTarget.dataset.key
      if(url == '/pages/user/user/post/index'){
        url = url + '?post='+this.data.form.post
      }
      wx.navigateTo({
        url: url
      })
    },
    onShareAppMessage: function() {
      return {
          title: "天意美业直通车",
          path: "/pages/index/index?puid="+wx.getStorageSync('uid')
      };
    },
    imgupload:function(e){
      this.setData({
        uploadtrue:true,
        type:e.target.dataset.type
    })
    },
    submit:function(e){
      var key = e.target.dataset.key;
      if(key == 1){
        var sourceType = 'camera'
      }
      if(key == 2){
        var sourceType = 'album'
      }
      var that = this
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: [sourceType], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          wx.uploadFile({
            url: that.data.imgUrl+'/api/img/upload',      //此处换上你的接口地址
            filePath: tempFilePaths[0],
            name: 'img',
            header: {  
              "Content-Type": "multipart/form-data",
              'accept': 'application/json',  //若有token，此处换上你的token，没有的话省略
              'token':'tymy'
            },
            formData:{},
            success: function(res){
              var form = that.data.form;
              form.realPic = res.data
              that.setData({
                img1:that.data.imgUrl+res.data,
                form:form,
                uploadtrue:false
              })
            },
            fail: function(res){
              console.log('fail');
            },
          })
        }
      })
    },
    submit3:function(){
      this.setData({
        uploadtrue:false
      })
    },
});